<?php

/**
 * This is the model class for table "baigiang".
 *
 * The followings are the available columns in table 'baigiang':
 * @property integer $id
 * @property string $name
 * @property string $linkurl
 * @property integer $creator
 * @property string $createdTime
 * @property string $modifiedTime
 * @property integer $khoihocId
 * @property integer $monhocId
 */
class BaiGiang extends CActiveRecord
{
        public $thumbnailFile;
        public $file;
    
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return BaiGiang the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'baigiang';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, creator, khoihocId, monhocId', 'required'),
			array('creator, khoihocId, monhocId, monhocChildId', 'numerical', 'integerOnly'=>true),
			array('name, linkurl, thumbnailUrl', 'length', 'max'=>255),
			array('createdTime, modifiedTime, description', 'safe'),
                        array('file', 'file', 'types'=>'zip', 'allowEmpty'=>true),
                        array('thumbnailFile', 'file', 'types'=>'jpg, jpeg, png, gif', 'allowEmpty'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, linkurl, creator, createdTime, modifiedTime, khoihocId, monhocId, monhocChildId', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'linkurl' => 'Link Bài Giảng',
			'creator' => 'Creator',
			'createdTime' => 'Created Time',
			'modifiedTime' => 'Modified Time',
			'khoihocId' => 'Khối Học',
			'monhocId' => 'Môn Học',
                        'monhocChildId' => 'Phân Loại Nhỏ',
                        'description'=>'Mô tả',
                        'file'=>'File Bài Giảng',
                        'thumbnailFile'=>'Thumbnail'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('linkurl',$this->linkurl,true);
		$criteria->compare('creator',$this->creator);
		$criteria->compare('createdTime',$this->createdTime,true);
		$criteria->compare('modifiedTime',$this->modifiedTime,true);
		$criteria->compare('khoihocId',$this->khoihocId);
		$criteria->compare('monhocId',$this->monhocId);
                $criteria->compare('monhocChildId',$this->monhocChildId);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        protected function beforeValidate() {
                $this->creator=Yii::app()->user->getState('userId');
                
                return true;
        }
        
        protected function beforeSave() {
                if($this->isNewRecord){
                        $this->createdTime=  new CDbExpression('NOW()');
                        $this->modifiedTime=$this->createdTime;
                }else{
                        $this->modifiedTime=new CDbExpression('NOW()');
                }
                
            
                return true;
        }
        
        public function getCreatorModel(){
                return User::model()->findByPk($this->creator);
        }
        
        public function getMonHocModel(){
                return MonHoc::model()->findByPk($this->monhocId);
        }
        
        public function getKhoiHocModel(){
                return KhoiHoc::model()->findByPk($this->khoihocId);
        }
        
        public function getMonhocChildModel(){
                if($this->monhocChildId)
                        return MonHocChild::model()->findByPk($this->monhocChildId);
                else
                        return null;
        }
}